package com.zxg.androidbuslearn

import android.app.Activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.system.Os.remove

import java.util.Stack

/**
 * 作者: 赵小刚
 * 生成时间: 2018/10/22.
 * 版本: 1.0
 * 简介: 基本Activity。统一管理Activity
 * 修改记录:
 */
abstract class BaseActivity : AppCompatActivity() {

    fun finishActivity(simpleName: String) {
        for (activity in mActivityStack) {
            if (activity.javaClass.simpleName === simpleName) {
                activity.finish()
                mActivityStack.remove(activity)
                break
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        put(this)
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onPause() {
        super.onPause()
    }

    override fun onStop() {
        super.onStop()
    }

    override fun onRestart() {
        super.onRestart()
    }

    override fun onDestroy() {
        super.onDestroy()
        remove(this)
    }

    companion object {
        private val TAG = "BaseActivity"

        internal var mActivityStack = Stack<Activity>()

        private fun put(activity: Activity) {
            mActivityStack.add(activity)
        }

        fun remove(activity: Activity) {
            mActivityStack.remove(activity)
        }

        // 删除所有实例
        fun finishAll() {
            while (!mActivityStack.empty()) {
                mActivityStack.pop().finish()
            }
        }

    }
}
